;*****************************************************
; 	Copyright (C) SEIKO EPSON CORP. 1997-2001
;	All rights Reserved
;
;	File name : strncpy.s
;	Function  :
;	       This is strncpy function
;	Revision  :
;		1997/03/24	V.Chan start
;		1997/03/26	fixed afterloop bug
;		1997/03/31	removed delayed jump after memory write (ln 41)
;		2001/12/06	T.Tazaki for GNU33(Linux)
;*****************************************************

;*****************************************************
; strncpy
;	string copy N bytes from src to dest
;	does not copy characters after a NULL
;
; arguments : %r6:dest addr, %r7:src addr, %r8:byte number
; return    : %r4:dest addr
;*****************************************************

	.section .text
	.align 1
	.global strncpy
	.type	strncpy,@function

strncpy:
	ld.w 	%r4, %r6		; return dest add

strncpy_loop:
	cmp		%r8, 0
	jreq	strncpy_ret
	ld.ub	%r10, [%r7]+	; copy src 1 byte to dest
	ld.b	[%r6]+, %r10
	cmp		%r10, 0
	jrne.d	strncpy_loop
	sub		%r8, 1			; decrement count

afterloop:
	cmp		%r8, 0			; if not N bytes copied fill with 0's
	jreq	strncpy_ret
	ld.b	[%r6]+, %r10
	sub		%r8, 1
	jp		afterloop

strncpy_ret:
	ret


